﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
namespace BenefitHelper.View
{
    /// <summary>
    /// 操作员日交易信息
    /// </summary>
    public class OperatorDayChange
    {
        public int Id { get; set; }
        /// <summary>
        /// 用户编号
        /// </summary>
        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        /// <summary>
        /// 交易日
        /// </summary>
        public int TradeHistoryId { get; set; }

        public virtual Sys.TradeHistory TradeHistory { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }
        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }
        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }
        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }
        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }
        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }

        /// <summary>
        /// 查询交易员交易基础数据(这个方法应该没问题)
        /// </summary>
        /// <returns></returns>
        public void InitOperatorDayChange(int tradeHistoryId, DB.DBManager db)
        {

            Sys.TradeHistory history = db.TradeHistorys.Find(tradeHistoryId);


            var querya = new Sys.Operator().GetUnDeletedOperators(db);


            foreach (Sys.Operator op in querya)
            {
                OperatorDayChange change = new OperatorDayChange();
                List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(history.GetPdateFromTradeHistoryId(tradeHistoryId, db), op.Id, db);
                change.OperatorId = op.Id;
                change.TradeHistoryId = tradeHistoryId;
                if (accounts.Count > 0)
                {
                    foreach (Sys.Account account in accounts)
                    {
                        var query = from t in db.AccountDayChange where t.AccountId == account.Id && t.TradeHistoryId == tradeHistoryId select t;
                        if (query.Count() > 0)
                        {
                            change.BillCount += query.First().BillCount;
                            change.DayCount += query.First().DayCount;
                            change.Free += query.First().Free;
                            change.LoseCount += query.First().LoseCount;
                            change.OperatorId = op.Id;
                            change.Profit += query.First().Profit;
                            change.SumLost += query.First().SumLost;
                            change.SumWin += query.First().SumWin;
                            change.TradeHistoryId = tradeHistoryId;
                            change.UsedMargin += query.First().UsedMargin;
                            change.WinCount += query.First().WinCount;
                        }
                        var _query = from t in db.AccountDayAnalysis where t.AccountId == account.Id && t.TradeHistoryId == tradeHistoryId select t;
                        if (_query.Count() > 0)
                        {
                            change.AvgBillBenefit += _query.First().AvgBillBenefit;
                            change.AvgPostionTime += _query.First().AvgPostionTime;
                            //if (_query.First().MaxReturn > change.MaxReturn)
                            //{
                            change.MaxReturn = _query.First().MaxReturn;
                            //}
                            change.OverStopLoss += _query.First().OverStopLoss;
                            change.PingJinPing += _query.First().PingJinPing;
                            change.RaiseRate += _query.First().RaiseRate;
                            change.RiskCount += _query.First().RiskCount;
                            change.Utilization += _query.First().Utilization;
                            change.WinRate += _query.First().WinRate;
                            change.Yield += _query.First().Yield;
                        }
                    }
                    change.AvgBillBenefit = Math.Round(change.AvgBillBenefit / accounts.Count, 2);
                    //   change.AvgBillBenefit.ToString().Debug();
                    change.AvgPostionTime = Math.Round(change.AvgPostionTime / accounts.Count, 2);
                    //   change.AvgPostionTime.ToString().Debug();
                    change.Utilization = Math.Round(change.Utilization / accounts.Count, 2);
                    //    change.Utilization.ToString().Debug();
                    change.WinRate = Math.Round(change.WinRate / accounts.Count, 2);
                    //  change.WinRate.ToString().Debug();
                }
                db.OperatorDayChange.Add(change);
            }
            db.SaveChanges();
        }



        /// <summary>
        /// 查询交易员交易基础数据(这个方法应该没问题)
        /// </summary>
        /// <returns></returns>
        public List<OperatorDayChange> GetOperatorDayChanges(int tradeHistoryId, DB.DBManager db)
        {
            //List<OperatorDayChange> changes = new List<OperatorDayChange>();
            //Sys.TradeHistory history = db.TradeHistorys.Find(tradeHistoryId);
            //List<Sys.Operator> operators = db.Operators.ToList();

            //foreach (Sys.Operator op in operators)
            //{
            //    View.OperatorDayChange change = GetOperatorDayChange(op.Id, tradeHistoryId, db);
            //    changes.Add(change);
            //}
            //return changes;
            var odc = db.OperatorDayChange.Where(a => a.TradeHistoryId == tradeHistoryId);
            return odc.ToList();
        }


        public OperatorDayChange GetOperatorDayChange(int operatorId, int tradeHistoryId, DB.DBManager db)
        {
            var query = db.OperatorDayChange.Where(a => a.OperatorId == operatorId).Where(a => a.TradeHistoryId == tradeHistoryId).Include(a => a.Operator);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return new OperatorDayChange();
            }
        }

        public void Delete(int tradeHistoryId, DB.DBManager db)
        {
            var query = db.OperatorDayChange.Where(a => a.TradeHistoryId == tradeHistoryId);
            foreach (OperatorDayChange c in query)
            {
                db.OperatorDayChange.Remove(c);
            }
            db.SaveChanges();
        }
    }
}
